package com.web.dao.impl;

import com.web.dao.StudentDAO;
import com.web.entity.Student;
import com.web.util.JDBCUtils;

import java.util.List;

public class StudentDAOImpl implements StudentDAO {
    @Override
    public List<Student> selectAllStudents() {
        return JDBCUtils.query(Student.class,"select * from student");
    }

    @Override
    public List<Student> selectByKeyWord(String keyword) {
        List<Student> stus = JDBCUtils.query(Student.class,
                "select * from student where name like ?","%" + keyword + "%");
        return stus;
    }


    @Override
    public Student selectById(String id) {
        List<Student> stus = JDBCUtils.query(Student.class,
                "select * from student where id = ?",id);
        Student stu = stus.get(0);
        return stu;
    }



    @Override
    public void addStudent(String name, String age,String cellphone,String address,String gender){
        JDBCUtils.update("insert into student(name,age,cellphone,address,gender) values(?,?,?,?,?)",
                name,age,cellphone,address,gender);
    }


    @Override
    public void updateStudent(String name, String age,String cellphone,String address,String gender,String id) {
        JDBCUtils.update("update student set name = ?,age = ?,cellphone = ?,address = ?,gender = ? where id = ?",
                name,age,cellphone,address,gender,id);
    }


    @Override
    public void deleteStudent(String id) {
        JDBCUtils.update("delete from student where id = ?",
                id);
    }

    @Override
    public List<Student> selectStudentPage(int start, int len) {
        return JDBCUtils.query(Student.class,
                "select * from student limit ?,?",start,len);
    }

    @Override
    public int selectStudentCount() {
        return JDBCUtils.queryCount("select count(*) from student");
    }

    @Override
    public  void insertStudent(Student student){
        JDBCUtils.update(
                "insert into student(name,age,gender,cellphone,address) values(?,?,?,?,?)",
                student.getName(),student.getAge(),student.getGender(),student.getCellphone(),student.getAddress()
        );
    }

    @Override
    public void updateStudent(Student student){
        JDBCUtils.update(
                "update student set name=?,age=?,gender=?,cellphone=?,address=? where id = ?",
                student.getName(),student.getAge(),student.getGender(),student.getCellphone(),student.getAddress(),student.getId());
    }

    @Override
    public void deleteStudent(int id) {
        JDBCUtils.update("delete from student where id =?",id);
    }

}